package routes

import (
	"fiberTest/internal/controller"
	"fiberTest/internal/model"
	"fiberTest/pkg/utils"
	"fmt"
	"github.com/brianvoe/gofakeit/v6"
	"github.com/gofiber/fiber/v2"
	"github.com/gofiber/fiber/v2/middleware/monitor"
	"log"
)

func RegisterTestRoute() {
	app := pkg.GetConfig().App

	// 匹配任何请求
	app.Use(func(c *fiber.Ctx) error {
		fmt.Println("匹配任何请求")
		// 执行下一步
		return c.Next()
	})

	// 匹配多个路径
	app.Use([]string{"/api", "/home", "prefix"}, func(c *fiber.Ctx) error {
		fmt.Println("匹配了多个路径")
		return c.Next()
	})

	// 添加多个处理器
	app.Use("/api", func(c *fiber.Ctx) error {
		fmt.Println("我是第一个处理器")
		c.Set("X-Custom-Header", gofakeit.RandomString([]string{"32"}))
		return c.Next()
	}, func(c *fiber.Ctx) error {
		fmt.Println("我是第二个处理器")
		return c.Next()
	})

	app.Get("/", controller.Hello).Name("Hello")

	app.Get("/user/:name/books/:title/:id", controller.Canshu)
	//app.Get("/user/+", controller.Jia)
	//app.Get("/user/:name?", controller.Option)

	//app.Get("/user/*", func(c *fiber.Ctx) error {
	//	return c.SendString(c.Params("*"))
	//})

	//app.Get("/head2", func(c *fiber.Ctx) error {
	//	// 获取ip
	//	ip := c.IP()
	//	log.Println("ip:", ip)
	//
	//	get := c.Get("Content-Type") // "text/plain"
	//	s := c.Get("CoNtEnT-TypE")   // "text/plain"
	//	// 第二个参数可以设置，参数默认值
	//	s2 := c.Get("something", "john") // "john"
	//	log.Println(get, s, s2)
	//	return c.SendString("yoooo")
	//})

	app.Hooks().OnName(func(r fiber.Route) error {
		fmt.Print("Name: " + r.Name + ", ")

		return nil
	})

	app.Hooks().OnName(func(r fiber.Route) error {
		fmt.Print("Method: " + r.Method + "\n")

		return nil
	})

	app.Get("/coo", func(c *fiber.Ctx) error {
		cookies := c.Cookies("name")   // "john"
		s := c.Cookies("empty", "doe") // "doe"
		log.Println(cookies, s)
		rep := cookies + " " + s
		return c.SendString(rep)
	}).Name("11")

	app.Get("body", controller.Body).Name("22")

	app.Get(`/v1/some/resource/name\:customVerb`, func(c *fiber.Ctx) error {
		return c.SendString("Hello, Community")
	}).Name("33")

	// 前缀
	group := app.Group("/prefix")
	group.Get("/hello", controller.Hello)
	// 主动抛异常 如何处理
	group.Get("/exce", controller.Exception)

	app.Get("/validateTest", controller.ValidateUser)

	//	response test

	app.Get("/json", func(c *fiber.Ctx) error {

		// 组装实体类
		data := model.SomeStruct{
			Name: "Grame",
			Age:  20,
		}

		// 通过json函数返回
		return c.JSON(data)
		// => Content-Type: application/json
		// => "{"Name": "Grame", "Age": 20}"

		//return c.JSON(fiber.Map{
		//	"name": "Grame",
		//	"age":  20,
		//})
		// => Content-Type: application/json
		// => "{"name": "Grame", "age": 20}"
	})

	app.Get("/jsonp", func(c *fiber.Ctx) error {
		// Create data struct:
		data := model.SomeStruct2{
			Name: "Grame",
			Age:  20,
		}

		return c.JSONP(data)
		//=> callback({"name": "Grame", "age": 20})

		//return c.JSONP(data, "customFunc")
		// => customFunc({"name": "Grame", "age": 20})
	})

	app.Get("/not-found", func(c *fiber.Ctx) error {
		// 设置返回状态码
		//return c.SendStatus(415)
		// => 415 "Unsupported Media Type"

		_ = c.SendString("Hello, World!")
		// 设置返回状态码
		return c.SendStatus(415)
		// => 415 "Hello, World!"
	})

	app.Get("/download", func(c *fiber.Ctx) error {
		//return c.Download("C:\\Users\\KONODIODA\\Pictures\\MSI Wallpaper\\MSI.png")
		// => Download report-12345.pdf

		return c.Download("C:\\Users\\KONODIODA\\Pictures\\MSI Wallpaper\\MSI.png", "report.pdf")
		// => Download report.pdf
	})

	app.Get("/setContent", func(c *fiber.Ctx) error {
		c.Set("Content-Type", "text/mySetting")
		// => "Content-type: text/plain"
		return c.SendStatus(200)
		// ...
	})

	app.Get("/", func(c *fiber.Ctx) error {
		return c.Redirect("/foo/bar")
		//return c.Redirect("../login")
		//return c.Redirect("http://example.com")
		//return c.Redirect("http://example.com", 301)
	})

	app.Get("/error", controller.Error)

	app.Post("/uploadFile", controller.Upload)

	app.Get("/downLoadErrorFile", controller.EDouwn)

	app.Get("/exce", controller.Exception2)

	// 一个监视器，可以观察当前设备的状态
	app.Get("/metrics", monitor.New())
}
